#include "scannercontrol.h"

ScannerControl* ScannerControl::m_instance = nullptr;
QMutex ScannerControl::m_mutex;

ScannerControl *ScannerControl::getInstance()
{
    if(!m_instance)
    {
        QMutexLocker locker(&m_mutex);
        if(!m_instance)
        {
            m_instance = new ScannerControl();
        }
    }
    return m_instance;
}

void ScannerControl::set_Connect_Parameters(QString port,
                                            QString baudRate,
                                            QString dataBit,
                                            QString parity,
                                            QString stopBit)
{
    QSerialPort::BaudRate set_BaudRate;
    QSerialPort::DataBits set_DataBit;
    QSerialPort::Parity   set_ParityBit;
    QSerialPort::StopBits set_StopBit;

    // 设置波特率
    if("115200" == baudRate)        set_BaudRate = QSerialPort::Baud115200;
    else if("57600" == baudRate)    set_BaudRate = QSerialPort::Baud57600;
    else if("38400" == baudRate)    set_BaudRate = QSerialPort::Baud38400;
    else if("19200" == baudRate)    set_BaudRate = QSerialPort::Baud19200;
    else if("9600" == baudRate)     set_BaudRate = QSerialPort::Baud9600;
    else if("4800" == baudRate)     set_BaudRate = QSerialPort::Baud4800;
    else if("2400" == baudRate)     set_BaudRate = QSerialPort::Baud2400;
    else                            set_BaudRate = QSerialPort::Baud1200;

    // 设置数据位
    if("8" == dataBit)              set_DataBit = QSerialPort::Data8;
    else if("7" == dataBit)         set_DataBit = QSerialPort::Data7;
    else if("6" == dataBit)         set_DataBit = QSerialPort::Data6;
    else                            set_DataBit = QSerialPort::Data5;

    // 设置校验位
    if("None" == parity || "N" == parity)         set_ParityBit = QSerialPort::NoParity;
    else if("Even" == parity || "E" == parity)    set_ParityBit = QSerialPort::EvenParity;
    else if("Odd" == parity || "O" == parity)     set_ParityBit = QSerialPort::OddParity;
    else if("Mark" == parity || "M" == parity)    set_ParityBit = QSerialPort::MarkParity;
    else                            set_ParityBit = QSerialPort::SpaceParity;

    // 设置停止位
    if("1" == stopBit)              set_StopBit = QSerialPort::OneStop;
    else if("1.5" == stopBit)       set_StopBit = QSerialPort::OneAndHalfStop;
    else                            set_StopBit = QSerialPort::TwoStop;

    // 应用串口设置
    m_Serial->setPortName(port);
    m_Serial->setBaudRate(set_BaudRate);
    m_Serial->setDataBits(set_DataBit);
    m_Serial->setParity(set_ParityBit);
    m_Serial->setStopBits(set_StopBit);
    // 设置流控制为无（默认值，显式设置更清晰）
    m_Serial->setFlowControl(QSerialPort::NoFlowControl);
}

void ScannerControl::connet_To_Device()
{
    if (m_Serial->isOpen()) {
        m_Serial->close(); // 先关闭已打开的连接
    }

    if(m_Serial->open(QIODevice::ReadWrite)) {
        qDebug() << "Cpp: 扫码枪 连接成功，端口：" << m_Serial->portName();
    } else {
        qDebug() << "Cpp: 扫码枪 连接失败，错误：" << m_Serial->errorString();
    }
}

ScannerControl::ScannerControl(QObject *parent)
    : QObject{parent}
{
    m_Serial = new QSerialPort();

    QObject::connect(m_Serial,&QSerialPort::readyRead,this,&ScannerControl::recv_Scanner_Result);

}

ScannerControl::~ScannerControl()
{

}

void ScannerControl::recv_Scanner_Result()
{
    QByteArray recvData = m_Serial->readAll();
    emit scanner_Recv_Data_Signal(recvData);
}

void ScannerControl::test_Recv_Slots(QByteArray &recvData)
{
    qDebug()<<"当前扫码枪数据 : "<<QString(recvData);
}
